<!--
* description: 车辆管理-销退入库
* author: ligy
* createdDate: 2020-11-20
-->
<template>
  <div class="app-container app-container-table">
    <one-table-template
      ref="multipleTable"
      :dynamic-buttons="tableButtons"
      :dynamic-components="tableComponents"
      :dynamic-api-config="dynamicApiConfig"
      :dynamic-table-cols="tableCols"
      :dynamic-form-fields="formField"
      :dynamic-is-show-select="true"
      :dynamic-is-init-table="true"
      :dynamic-api-style="'restful'"
      :callback-obj="callbackObj"
    />
  </div>
</template>

<script>
import { oneTableWithViewTemplateMixins } from '@/components/mixins/oneTableWithViewTemplateMixins'
import OneTableTemplate from '@/components/templates/oneTable'
import api from '@/api/vehicleManagement'
import lov from '@/api/Lov'

export default {
  components: {
    OneTableTemplate
  },
  mixins: [oneTableWithViewTemplateMixins],
  data() {
    return {
      dynamicApiConfig: api.salesReturnstock,
      // 动态生成网格列
      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          : [
            {
              prop: 'releaseId',
              label: '主键ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'updateControlId',
              label: '并发控制ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'returnUpdateControlId',
              label: '销退并发控制ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'saleOrderTypeId',
              label: '销退类型Id',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'saleOrderRetrunId',
              label: '销退钉单ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'returnState',
              label: '退订状态',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'orderStatus',
              label: '订单状态',
              hidden: true,
              alwaysHidden: true
            },
            // 操作
            {
              prop: 'operating',
              label: '操作',
              align: 'center',
              fixed: false,
              isComponent: true,
              comps: [
                {
                  compKey: 'propKey0',
                  labelName: '销退入库',
                  codeField: '',
                  clickEvent: index => this.edit(index),
                  component: () => import('@/components/org/linkButton')
                }
              ]
            },
            {
              prop: 'saleOrderReturnCode',
              label: '销退申请单号',
              align: 'center',
              width: 140
            },
            {
              prop: 'saleOrderType',
              label: '销退类型',
              align: 'center'
            },
            {
              prop: 'saleOrderCode',
              label: '订单号',
              align: 'center',
              width: 140
            },
            {
              prop: 'saleOrderDate',
              label: '订单日期',
              align: 'center',
              width: 140
            },
            {
              prop: 'custName',
              label: '客户名称',
              align: 'center'
            },
            {
              prop: 'custTel',
              label: '客户电话',
              align: 'center',
              width: 140
            },
            {
              prop: 'VIN',
              label: 'VIN',
              align: 'center',
              width: 140
            },
            { prop: 'carSeriesCode',
              label: '车系编码',
              align: 'center'
            },
            { prop: 'carSeriesCn',
              label: '车系',
              align: 'center'
            },
            { prop: 'carConfigCode',
              label: '基础车型编码',
              align: 'center'
            },
            {
              prop: 'carConfigCn',
              label: '基础车型',
              align: 'center'
            },
            {
              prop: 'optionalPagName',
              label: '选装包',
              align: 'center'
            },
            {
              prop: 'carColorName',
              label: '车身颜色',
              align: 'center'
            },
            {
              prop: 'carIncolorName',
              label: '内饰颜色',
              align: 'center'
            }
          ],
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
            .tableComponents
          : [
            // 订单号
            {
              compKey: 'compKey1',
              labelName: '订单号：',
              codeField: 'saleOrderCode',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true
            },
            // 销退单号
            {
              compKey: 'compKey2',
              labelName: '销退单号：',
              codeField: 'saleOrderReturnCode',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true
            },
            // VIN码
            {
              compKey: 'compKey3',
              labelName: 'VIN码：',
              codeField: 'VIN',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true
            },
            {
              compKey: 'compKey7',
              labelName: '车系：',
              codeField: 'carSeriesId',
              oFields: 'carSeriesId,carSeriesCn',
              customObj: {
                api: lov.listOfCarSeries
              },
              selectType: '1',
              component: () => import('@/components/org/commonSelect'),
              type: 'dropdownList',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey8',
              labelName: '基础车型：',
              codeField: 'carConfigId',
              component: () => import('@/components/org/commonSelect'),
              oFields: 'carConfigId,carConfigCn',
              parentFileds: 'value:carSeriesId',
              customObj: {
                api: lov.queryCarType,
                listQuery: {
                  carSeriesId: ''
                },
                selectType: '1'
              },
              type: 'dropdownList',
              isMul: false,
              isMust: false
            },
            {
              compKey: 'compKey9',
              labelName: '选装包：',
              codeField: 'optionalPagId',
              component: () => import('@/components/org/commonSelect'),
              oFields: 'optionalPagId,optionalPagName',
              parentFileds: 'value:carConfigId',
              customObj: {
                api: lov.queryCarOptionalPagLinkMessage,
                listQuery: {
                  carConfigId: ''
                },
                selectType: '1'
              },
              type: 'dropdownList',
              isMul: false,
              isMust: false
            },
            {
              compKey: 'compKey10',
              labelName: '车身颜色：',
              codeField: 'carColorId',
              component: () => import('@/components/org/commonSelect'),
              oFields: 'carColorId,carColorName',
              parentFileds: 'value:carConfigId|carSeriesId|optionalPagId',
              customObj: {
                listQuery: {
                  carConfigId: '', carSeriesId: '', optionalPagId: ''
                },
                api: lov.queryCarColorLinkMessage,
                selectType: '1'
              },
              type: 'dropdownList',
              isMul: false,
              isMust: false
            },
            {
              compKey: 'compKey11',
              labelName: '内饰颜色：',
              codeField: 'carIncolorId',
              component: () => import('@/components/org/commonSelect'),
              oFields: 'carIncolorId,carIncolorName',
              parentFileds: 'value:carConfigId|carSeriesId|optionalPagId',
              customObj: {
                listQuery: {
                  carConfigId: '', carSeriesId: '', optionalPagId: ''
                },
                api: lov.queryCarInColorLinkMessage,
                selectType: '1'
              },
              type: 'dropdownList',
              isMul: false,
              isMust: false
            }
          ],
      tableButtons: [
        {
          conmpKey: 'btenKey1',
          size: 'small',
          text: '批量入库',
          clickEvent: () => this.send(),
          position: 'left',
          disabled: true
        },
        {
          conmpKey: 'btenKey2',
          type: 'primary',
          size: 'small',
          text: '查询',
          clickEvent: () => this.queryTable(1),
          position: 'right'
        },
        {
          conmpKey: 'btenKey3',
          size: 'small',
          text: '重置',
          clickEvent: () => this.reset(),
          position: 'right'
        }
      ],
      formField: this.$utils.getFormField(this, {
        saleOrderCode: '',
        saleOrderReturnCode: '',
        vinCode: '',
        modelConfiguration: '',
        carSeriesId: '',
        carConfigId: '',
        optionalPagId: '',
        carColorId: '',
        carIncolorId: '',
        VIN: ''
      }),
      selectList: [],
      callbackObj: {
        // 当选择项发生变化时会触发该事件（多选时）
        onCheckSelectChange: (selection) => {
          this.selectList = selection
          console.log(this.selectList)
          this.handleSelectionChange(selection)
        }
      }
    }
  },
  methods: {
    send() {
      this.$confirm('是否确定要批量入库', '提示', {
        type: 'warning'
      }).then(() => {
        var selectRow = {
          updateControlDId: [],
          updateControlId: [],
          deliveryDlrId: [],
          salePlanCode: [],
          abnormalType: [],
          CAR_ID: [],
          UPDATE_CONTROL_ID: [],
          saleOrderReturnCode: [],
          auditDate: [],
          saleOrderFee: [],
          saleOrderCodeC: [],
          saleOrderReason: [],
          saleOrderId: [],
          saleOrderTypeId: [],
          returnUpdateControlId: [],
          saleOrderRetrunId: [],
          carUpdateControlId: [],
          VIN: []
        }
        for (const item in selectRow) {
          this.selectList.forEach(a => {
            selectRow[item].push(a[item])
          })
        }
        const queryObj = {
        // 保存mutation
          type: 'mutation',
          // api配置
          apiConfig: api.upWarehousing,
          variables: {
            updateControlDId: selectRow.updateControlDId,
            updateControlId: selectRow.updateControlId,
            deliveryDlrId: selectRow.deliveryDlrId,
            salePlanCode: selectRow.salePlanCode,
            abnormalType: selectRow.abnormalType,
            CAR_ID: selectRow.CAR_ID,
            UPDATE_CONTROL_ID: selectRow.UPDATE_CONTROL_ID,
            saleOrderReturnCode: selectRow.saleOrderReturnCode,
            auditDate: selectRow.auditDate,
            saleOrderFee: selectRow.saleOrderFee,
            saleOrderCodeC: selectRow.saleOrderCodeC,
            saleOrderReason: selectRow.saleOrderReason,
            saleOrderId: selectRow.saleOrderId,
            saleOrderTypeId: selectRow.saleOrderTypeId,
            returnUpdateControlId: selectRow.returnUpdateControlId,
            saleOrderRetrunId: selectRow.saleOrderRetrunId,
            carUpdateControlId: selectRow.carUpdateControlId,
            VIN: selectRow.VIN
          }
        }
        var paramD = this.$getParams(queryObj)
        this.$requestAPI(paramD).then(response => {
          if (response.result === '1') {
            this.$message({
              message: response.msg
            })
            this.$nextTick(() => {
              this.$refs.multipleTable.queryTable(1)
            })
          } else {
            this.$message({
              message: response.msg
            })
          }
        }).catch(err => {
          console.log(err)
        })
      })
    },
    edit(index) {
      this.$confirm('是否确定要销退入库', '提示', {
        type: 'warning'
      }).then(() => {
        const { deliveryDlrId, salePlanCode, abnormalType, updateControlId, saleOrderId, saleOrderTypeId, returnUpdateControlId, saleOrderRetrunId, VIN, carUpdateControlId, auditDate, saleOrderFee, saleOrderReturnCode, saleOrderCodeC, saleOrderReason, CAR_ID, UPDATE_CONTROL_ID, updateControlDId } = this.$refs.multipleTable.list[index]
        const queryObj = {
        // 保存mutation
          type: 'mutation',
          // api配置
          apiConfig: api.upWarehousing,
          variables: {
            updateControlId: [updateControlId],
            updateControlDId: [updateControlDId],
            auditDate: [auditDate],
            saleOrderFee: [saleOrderFee],
            saleOrderReturnCode: [saleOrderReturnCode],
            saleOrderCodeC: [saleOrderCodeC],
            saleOrderReason: [saleOrderReason],
            saleOrderId: [saleOrderId],
            saleOrderTypeId: [saleOrderTypeId],
            returnUpdateControlId: [returnUpdateControlId],
            saleOrderRetrunId: [saleOrderRetrunId],
            carUpdateControlId: [carUpdateControlId],
            CAR_ID: [CAR_ID],
            deliveryDlrId: [deliveryDlrId],
            salePlanCode: [salePlanCode],
            abnormalType: [abnormalType],
            UPDATE_CONTROL_ID: [UPDATE_CONTROL_ID],
            VIN: [VIN]
          }
        }
        var paramD = this.$getParams(queryObj)
        this.$requestAPI(paramD).then(response => {
          if (response.result === '1') {
            this.$message({
              message: response.msg
            })
            this.$nextTick(() => {
              this.$refs.multipleTable.queryTable(1)
            })
          } else {
            this.$message({
              message: response.msg
            })
          }
        }).catch(err => {
          console.log(err)
        })
      })
    },
    // 当选择项发生变化时会触发该事件
    handleSelectionChange(selection) {
      const btns = this.tableButtons.filter(o => o.disabled !== undefined)
      if (btns.length > 0) {
        const isCheck = selection && selection.length > 0
        btns.forEach((item) => {
          item.disabled = !isCheck
        })
      }
    }
  }
}
</script>

<style></style>
